package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysWhitePaperFile;
import com.ruoyi.system.domain.vo.SysWhitePaperOutlineVo;
import com.ruoyi.system.service.ISysWhitePaperFileService;
import com.ruoyi.system.service.ISysWhitePaperOutlineService;
import com.ruoyi.system.utils.PdfBoxGenerator;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWhitePaperProjectMapper;
import com.ruoyi.system.domain.SysWhitePaperProject;
import com.ruoyi.system.service.ISysWhitePaperProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白皮书基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-18
 */
@Service
public class SysWhitePaperProjectServiceImpl implements ISysWhitePaperProjectService
{
    @Autowired
    private SysWhitePaperProjectMapper sysWhitePaperProjectMapper;
    @Autowired
    private ISysWhitePaperFileService sysWhitePaperFileService;
    @Autowired
    private ISysWhitePaperOutlineService sysWhitePaperOutlineService;
    @Autowired
    private PdfBoxGenerator pdfBoxGenerator;

    /**
     * 查询白皮书基本信息
     *
     * @param id 白皮书基本信息主键
     * @return 白皮书基本信息
     */
    @Override
    public SysWhitePaperProject selectSysWhitePaperProjectById(Long id)
    {
        SysWhitePaperProject whitePaperProject = sysWhitePaperProjectMapper.selectSysWhitePaperProjectById(id);

        // 根据项目ID查询其对应的附件信息
        SysWhitePaperFile sysWhitePaperFile = new SysWhitePaperFile();
        sysWhitePaperFile.setProjectId(id);
        List<SysWhitePaperFile> whitePaperFileList = sysWhitePaperFileService.selectSysWhitePaperFileList(sysWhitePaperFile);

        whitePaperProject.setFileList(whitePaperFileList);

        return whitePaperProject;
    }

    /**
     * 查询白皮书基本信息列表
     */
    @Override
    public List<SysWhitePaperProject> selectSysWhitePaperProjectList()
    {
        String userName = SecurityUtils.getUsername();
        return sysWhitePaperProjectMapper.selectSysWhitePaperProjectList(userName);
    }

    /**
     * 新增白皮书基本信息
     *
     * @param sysWhitePaperProject 白皮书基本信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertSysWhitePaperProject(SysWhitePaperProject sysWhitePaperProject)
    {
        String loginUserName = SecurityUtils.getUsername();
        Date currentDate = DateUtils.getNowDate();

        if (sysWhitePaperProject.getId() != null) {// 更新
            sysWhitePaperProject.setUpdateBy(loginUserName);
            sysWhitePaperProject.setUpdateTime(currentDate);
            sysWhitePaperProjectMapper.updateSysWhitePaperProject(sysWhitePaperProject);

            // 删除附件信息
            sysWhitePaperFileService.deleteSysWhitePaperFileByProjectId(sysWhitePaperProject.getId());
        } else {// 新增
            // 新增项目信息
            sysWhitePaperProject.setDelFlag("1");// 初建项目设为已删除；完成白皮书创建最后一步更新为未删除
            sysWhitePaperProject.setCreateBy(loginUserName);
            sysWhitePaperProject.setCreateTime(currentDate);
            sysWhitePaperProjectMapper.insertSysWhitePaperProject(sysWhitePaperProject);
        }

        // 新增附件信息
        List<SysWhitePaperFile> whitePaperFileList = sysWhitePaperProject.getFileList();
        if (CollectionUtils.isNotEmpty(whitePaperFileList)) {
            whitePaperFileList.forEach(file -> {
                file.setProjectId(sysWhitePaperProject.getId());
                file.setCreateBy(loginUserName);
                file.setCreateTime(currentDate);
            });

            sysWhitePaperFileService.batchInsertSysWhitePaperFile(whitePaperFileList);
        }

        return sysWhitePaperProject.getId();
    }

    /**
     * 修改白皮书基本信息
     *
     * @param sysWhitePaperProject 白皮书基本信息
     * @return 结果
     */
    @Override
    public int updateSysWhitePaperProject(SysWhitePaperProject sysWhitePaperProject)
    {
        sysWhitePaperProject.setUpdateTime(DateUtils.getNowDate());
        return sysWhitePaperProjectMapper.updateSysWhitePaperProject(sysWhitePaperProject);
    }

    /**
     * 批量删除白皮书基本信息
     *
     * @param ids 需要删除的白皮书基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperProjectByIds(Long[] ids)
    {
        return sysWhitePaperProjectMapper.deleteSysWhitePaperProjectByIds(ids);
    }

    /**
     * 删除白皮书基本信息信息
     *
     * @param id 白皮书基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperProjectById(Long id)
    {
        return sysWhitePaperProjectMapper.deleteSysWhitePaperProjectById(id);
    }

    @Override
    public byte[] generatePdfBytes(Long id) throws Exception {
        // 根据项目ID获取章节、标题及汇编内容
        List<SysWhitePaperOutlineVo> whitePaperOutlineVoList = sysWhitePaperOutlineService.selectSysWhitePaperOutlineList(id);

        // 生成白皮书PDF
        if (CollectionUtils.isNotEmpty(whitePaperOutlineVoList)) {
            byte[] pdfBytes = pdfBoxGenerator.createSimplePdf(whitePaperOutlineVoList);

            return pdfBytes;
        } else {
            return new byte[0];
        }
    }
}
